using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using System.Globalization;

namespace MobileStreams
{
    public partial class frmOverviewCategories : Form
    {
        // Specifies if Audio(Radio) streams or Video(TV) streams are displayed
        private ViewTypes _currentType;
        
        // Key and Description of all Categories
        private Hashtable hashCategories = null;

        private ViewTypes currentType
        {
            get
            {
                return _currentType;
            }
            set
            {
                // Show selected ViewType in the UI
                if (value == ViewTypes.Audio)
                {
                    menuitemViewAudio.Checked = true;
                    menuitemViewVideo.Checked = false;
                }
                else
                {
                    menuitemViewVideo.Checked = true;
                    menuitemViewAudio.Checked = false;
                }

                // Set Registry-Value
                Microsoft.Win32.Registry.CurrentUser.SetValue("Software\\MobileMediaStreams\\currView", value);

                _currentType = value;
            }
         }

        /// <summary>
        /// Constructor
        /// </summary>
        public frmOverviewCategories()
        {
            InitializeComponent();

//            CurrentUICulture
        }

        /// <summary>
        /// Button to Quit the Application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuitemQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Loads all the available Categories into the hashtable "hashCategories"
        /// </summary>
        private void LoadCategories()
        {
            // Reset Categories
            hashCategories = new Hashtable();

            // Used to read Categories from Textfile
            StreamReader srCat;

            // Open specific Text-File in StreamReader
            if (currentType == ViewTypes.Video)
            {
                srCat = File.OpenText(XMLHandling.strAppDir + "\\CategoriesVideo.txt");
                columnheaderView.Text = "Video";
            }
            else
            {
                srCat = File.OpenText(XMLHandling.strAppDir + "\\CategoriesAudio.txt");
                columnheaderView.Text = "Audio";
            }

            // Read first Line in Textfile
            string strLine = srCat.ReadLine();
            
            // Index of line in textfile
            int i = 0;

            // Read all lines into hashTable
            while (strLine != null)
            {
                // Key = Index in textfile
                hashCategories.Add(i, strLine);
                strLine = srCat.ReadLine();

                i++;
            }

            // Close StreamReader to unlock file
            srCat.Close();

            // Remove all previous Categories/Channels
            lvCategories.Items.Clear();

            // Add all the Categories into Listview
            for (int iC = 0; iC < hashCategories.Count; iC++)
            {
                ListViewItem lvi = new ListViewItem(iC.ToString());

                lvi.SubItems.Add(hashCategories[iC].ToString());
                lvCategories.Items.Add(lvi);

                // Select first Item
                if (iC == 0) 
                { 
                    lvi.Selected = true;
                    lvi.Focused = true;
                }
            }
        }
        
        /// <summary>
        /// Will be executed upon Loading of the Form-Window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmOverview_Load(object sender, EventArgs e)
        {
            // Display the ViewType from last time
            try
            {
                // The ViewType has been saved as string
                string sView = Microsoft.Win32.Registry.CurrentUser.GetValue("Software\\MobileMediaStreams\\currView").ToString();
                if (sView == "Video")
                {
                    currentType = ViewTypes.Video;
                }
                else
                {
                    currentType = ViewTypes.Audio;
                }
            }
            catch (Exception)
            {
                currentType = ViewTypes.Audio;   
            }

            // Load all Categories into the ListView
            LoadCategories();
        }

        /// <summary>
        /// Lists all Video-Streams
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuitemViewVideo_Click(object sender, EventArgs e)
        {
            currentType = ViewTypes.Video;
            LoadCategories();
        }

        /// <summary>
        /// Lists all Audio-Streams
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuitemViewAudio_Click(object sender, EventArgs e)
        {
            currentType = ViewTypes.Audio;
            LoadCategories();
        }

        /// <summary>
        /// Will be executed after user has selected Category
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuitemSELECT_Click(object sender, EventArgs e)
        {

            // Get Key of selected Category
            int iSelectedCategory = Convert.ToInt32(lvCategories.FocusedItem.Text);

            // Check if this is the category for custom channels (==Always last category (999))
            if (lvCategories.FocusedItem.Index + 1 == lvCategories.Items.Count)
            {
                iSelectedCategory = 999;
            }

            // Set the selected type (Audio or Video?)
            int iSelectedType = 0;
            if (currentType == ViewTypes.Audio) { iSelectedType = 0; }
            else if (currentType == ViewTypes.Video) { iSelectedType = 1; }

            frmOverviewChannels frm = new frmOverviewChannels(iSelectedCategory, iSelectedType);

            frm.ShowDialog();
            



            //currentChannels = new Channel[4];
            //Channel cn = new Channel();
            //cn.ID = 0;
            //cn.ChannelName = "Giga";
            //cn.Description = "Games TV";
            //cn.ChannelLogo = "abc.jpg";
            //cn.URL = "http://www.giga.de/";
            //cn.Category = 2;
            //Stream[] str = new Stream[2];
            //Stream str1 = new Stream();
            //Stream str2 = new Stream();
            //str1.Description = "Low";
            //str2.Description = "High";
            //str1.URL = "mms://giga.low.de/";
            //str2.URL = "mms://giga.high.de/";
            //str[0] = str1;
            //str[1] = str2;
            //cn.Streams = str;
            ////currentChannels[0] = new Channel(0, "Giga", "Games TV", "abc.jpg", "http://www.giga.de/", 2);
            //currentChannels[0] = cn;
            //// Serialization
            //XmlSerializer s = new XmlSerializer(typeof(Channel[]));
            //TextWriter w = new StreamWriter(strAppDir + "\\list.xml");
            //s.Serialize(w, currentChannels);
            //w.Close();
        }


        /// <summary>
        /// Will be executed after user pressed the Settings-Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuitemSettings_Click(object sender, EventArgs e)
        {
            frmSettings frm = new frmSettings();
            frm.ShowDialog();
        }

        /// <summary>
        /// Directly jump into Channel-View if user pressed number
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvChannels_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Will include number, that user has pressed
            int PressedNumber = 0;
            try
            {
                PressedNumber = Convert.ToInt32(e.KeyChar.ToString());
            }
            catch (Exception)
            {
                // What should I do here?
                // There's no need to throw an exception, as we now what caused this...
            }

            // Make sure number was pressed and number lies within range
            if (PressedNumber > 0 && PressedNumber <= lvCategories.Items.Count)
            {
                // Select and Focus Category
                ListViewItem lvi = lvCategories.Items[PressedNumber];
                lvi.Selected = true;
                lvi.Focused = true;

                // Display channels
                menuitemSELECT_Click(null, null);   
            }
        }

        private void menuitemNewChannel_Click(object sender, EventArgs e)
        {
            //frmNewChannel frm = new frmNewChannel();

            //frm.ShowDialog();

            MessageBox.Show("Not implemented yet");

        }

        /// <summary>
        /// Will be executed when the user presses a button -> this method handles KeyChar 14 (=Return/Enter)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmOverviewCategories_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                menuitemSELECT_Click(null, null);
            }
        }

        private void menuitemSearchUpdates_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet");
        }

        private void menuitemAbout_Click(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.ShowDialog();
        }
    }
}